package com.ctgu.chatapp.Netty.Handler

import com.ctgu.chatapp.Netty.protocol.Message
import com.ctgu.chatapp.Service.AckService
import com.ctgu.chatapp.Service.NotifyService
import com.ctgu.chatapp.model.Database.DataBaseManager
import com.ctgu.chatapp.view.IMSApplication
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler


@Sharable
class ChatResponseHandler : SimpleChannelInboundHandler<Message.IMSMsg?>() {

    private val notifyService = NotifyService()

    private val ackService = AckService()

    val dataBase by lazy {
        DataBaseManager.getInstance(IMSApplication.context)
    }

    /**
     * 0表示发送登陆消息,1表示返回的登陆消息,2为单聊的发送消息,3为单聊的返回消息
     * */
    override fun messageReceived(ctx: ChannelHandlerContext, msg: Message.IMSMsg?) {
        when (msg!!.head.msgType) {
            Message.Head.MsgType.Acknowledge -> {
                ackService.solve(msg)
            }
            Message.Head.MsgType.Notify -> {
                notifyService.solve(msg)
            }
            else -> {}
        }
    }
}